<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
	"http://www.w3.org/TR/html4/loose.dtd">
<html debug="true">
<head>
	<title>dojox.grid.Grid Test: Mysql Table Editing</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
	<style>
		@import "../resources/Grid.css";
		@import "../resources/tundraGrid.css";
		@import "../../../dojo/resources/dojo.css";
		@import "../../../dijit/themes/tundra/tundra.css";
		@import "../../../dijit/tests/css/dijitTests.css";

		.grid {
			height: 30em;
		}
	</style>

	<script type="text/javascript" src="../../../dojo/dojo.js" 
		data-dojo-config="isDebug:true, parseOnLoad: true"></script>
	<script type="text/javascript">
		dojo.require("dijit.dijit");
		dojo.require("dojox.grid.DataGrid");
		dojo.require("dojox.data.QueryReadStore");
		dojo.require("dojo.parser");
	</script>
	<script type="text/javascript">
		dojo.declare("DbStore", dojox.data.QueryReadStore, {
			fetch: function(request){
				request.serverQuery = {
					database: this.database || '',
					table: this.table || ''
				}
			}
		});
		var model = new dojox.grid._data.DbTable(null, null, 'support/data.php', "test", "testtbl");
		// simple display of row info; based on model observing.
		modelChange = function() {
			dojo.byId('rowCount').innerHTML = model.count + ' row(s)';
		}
		model.observer(this);
		
		// yay, let's deal with MySql date types, at least a little bit...
		// NOTE: supports only default date formatting YYYY-MM-DD HH:MM:SS or YY-MM-DD HH:MM:SS
		mysqlDateToJsDate = function(inMysqlDateTime, inDateDelim, inTimeDelim) {
			var dt = inMysqlDateTime.split(' '), d = dt[0], t = dt[1], r;
			d = d&&d.split(inDateDelim||'-');
			t = t&&t.split(inTimeDelim||':');
			if (d && d.length == 3) {
				r = new Date();
				r.setYear(d[0]);
				r.setMonth(parseInt(d[1])-1);
				r.setDate(d[2]);
			}
			if (t && t.length == 3) {
				r = r || new Date();
				r.setHours(t[0]);
				r.setMinutes(t[1]);
				r.setSeconds(t[2]);
			}
			return r || new Date(inMysqlDateTime);
		}
		
		jsDateToMySqlDate = function(inDate) {
			var
				d = new Date(inDate),
				y = d.getFullYear(),
				m = dojo.string.pad(d.getMonth() + 1),
				dd = dojo.string.pad(d.getDate())
			return dojo.string.substitute("${0}-${1}-${2}",[y, m, dd]);
		};
		
		// custom simple MySql date formatter
		formatMySqlDate = function(inDatum) {
			return inDatum != dojox.grid.na ? dojo.date.locale.format(mysqlDateToJsDate(inDatum), this.constraint) : dojox.grid.na;
		}
		
		// custom simple MySql date editor
		dojo.declare("mySqlDateCell", dojox.grid.cells.DateTextBox, {
			format: function(inDatum, inRowIndex){
				inDatum = mysqlDateToJsDate(inDatum);
				return this.inherited(arguments, [inDatum, inRowIndex]);
			},
			getValue: function(inRowIndex){
				var v = this.editor.getValue(), fv = jsDateToMySqlDate(v);
				return fv;
			}
		});
		
		var gridLayout = [ 
			{ type: "dojox.grid._RowSelector", width: "20px" },
			{
			defaultCell: { width: 6, type: dojox.grid.cells._Widget, editable: true }, 
			cells: [[
				{ name: 'Id', styles: 'text-align: right;', widgetClass: dijit.form.NumberTextBox }, 
				{ name: 'Name', width: 20},
				{ name: 'Message', styles: 'text-align: right;'},
				{ name: 'Date', 
					type: mySqlDateEditor, 
					formatter: formatMySqlDate, 
					constraint: {selector: "date"}, 
					width: 10, 
					styles: 'text-align:right;'}
			]]}
		];

		function waitMessage() {
			alert('Edit in progress, please wait.');
		}
		
		function getDefaultRow() {
			return ['', '', '', jsDateToMySqlDate(new Date())];
		}
		function addRow() {
			if(model.canModify()){
				grid.addRow(getDefaultRow());
			}else{
				waitMessage();
			}
		}
		
		function removeSelected(){
			if(model.canModify()){
				grid.removeSelectedRows();
			}else{
				waitMessage();
			}
		}
	</script>
</head>
<body class="tundra">
	<h1>dojox.grid.Grid Test: Mysql Table Editing</h1>
	<br>
	<button onclick="addRow()">Add Row</button>&nbsp;&nbsp;
	<button onclick="removeSelected()">Remove Selected</button>&nbsp;&nbsp;
	<button onclick="grid.edit.apply()">Apply Edit</button>&nbsp;&nbsp;
	<button onclick="grid.edit.cancel()">Cancel Edit</button>&nbsp;&nbsp;
	<button onclick="grid.render()">Refresh</button>
	<br><br>
	<div data-dojo-id="grid" class="grid" structure="gridLayout" dojoType="dojox.grid.Grid"
		store="model" singleClickEdit="true" autoWidth="true"></div>
	<div id="rowCount"></div>
	<p>Note: This test requires MySql and PHP and works with the database table available in support/testtbl.sql.</p>
</body>
</html>
